package com.heima.user.service.impl;
import java.util.Date;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.constants.message.FollowBehaviorConstants;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.feign.ApAuthorFeign;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserRelationService;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ApUserRelationServiceImpl  implements ApUserRelationService {
    @Autowired
    ApAuthorFeign apAuthorFeign;

    @Autowired
    ApUserMapper apUserMapper;
    
    @Autowired
    ApUserFanMapper apUserFanMapper;
    @Autowired
    ApUserFollowMapper apUserFollowMapper;
    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;

    @Override
    public ResponseResult follow(UserRelationDto dto) {
        if (dto==null||dto.getArticleId()==null||dto.getAuthorId()==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        if (dto.getOperation()==null||(dto.getOperation()!=0&&dto.getOperation()!=1))
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        ApAuthor apauthor = apAuthorFeign.findByUserId(dto.getAuthorId());
        if (apauthor==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"该作者不存在");
        ApUser articleUser = apUserMapper.selectById(apauthor.getUserId());
        if (AppThreadLocalUtils.getUser()==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        ApUser apUser = apUserMapper.selectById(AppThreadLocalUtils.getUser().getId());
        if (apUser==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"当前登陆用户不存在");
        if (dto.getOperation()==0)
            return followAuthor(apUser,articleUser,dto.getArticleId());

        return cancelFollowAuthor(apUser,articleUser);

    }

    private ResponseResult cancelFollowAuthor(ApUser apUser, ApUser articleUser) {
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId, articleUser.getId()).eq(ApUserFollow::getUserId, apUser.getId()));
        if (apUserFollow==null)
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"你没关注过该作者");
        apUserFollowMapper.deleteById(apUserFollow.getId());
        apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId,articleUser.getId()).eq(ApUserFan::getUserId,apUser.getId()));
        return ResponseResult.okResult();
    }

    private ResponseResult followAuthor(ApUser apUser, ApUser articleUser,Long articleId) {
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getFollowId, articleUser.getId()).eq(ApUserFollow::getUserId, apUser.getId()));
        if (apUserFollow!=null)
            return  ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"您已关注过该作者");
        apUserFollow = new ApUserFollow();
        apUserFollow.setUserId(apUser.getId());
        apUserFollow.setFollowId(articleUser.getId());
        apUserFollow.setFollowName(articleUser.getName());
        apUserFollow.setLevel((short)0);
        apUserFollow.setIsNotice(false);
        apUserFollow.setCreatedTime(new Date());
        apUserFollowMapper.insert(apUserFollow);


        ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId, apUser.getId()).eq(ApUserFan::getUserId, articleUser.getId()));
        if (apUserFan==null ){
        apUserFan = new ApUserFan();
        apUserFan.setUserId(articleUser.getId());
        apUserFan.setFansId(Long.valueOf(apUser.getId()));
        apUserFan.setFansName(apUser.getName());
        apUserFan.setLevel((short)0);
        apUserFan.setCreatedTime(new Date());
        apUserFan.setIsDisplay(false);
        apUserFan.setIsShieldLetter(false);
        apUserFan.setIsShieldComment(false);
        apUserFanMapper.insert(apUserFan);
        }


        FollowBehaviorDto followBehaviorDto = new FollowBehaviorDto();

        //给behavior模块发消息，保存收藏信息
        followBehaviorDto.setArticleId(articleId);
        followBehaviorDto.setFollowId(articleUser.getId());
        followBehaviorDto.setUserId(apUser.getId());
        kafkaTemplate.send(FollowBehaviorConstants.FOLLOW_BEHAVIOR_TOPIC, JSON.toJSONString(followBehaviorDto));

        return ResponseResult.okResult();
    }
}
